<template>
  <div class="search-container">
    <!-- 搜索头部 -->
    <div class="search-header">
      <div class="search-input">
        <el-input
          v-model="keyword"
          placeholder="搜索课程、文章、教师..."
          clearable
          @keyup.enter="handleSearch"
        >
          <template #prefix>
            <el-icon><Search /></el-icon>
          </template>
          <template #append>
            <el-button type="primary" @click="handleSearch">
              搜索
            </el-button>
          </template>
        </el-input>
      </div>

      <div class="search-tags">
        <el-tag
          v-for="tag in hotTags"
          :key="tag"
          class="hot-tag"
          @click="handleTagClick(tag)"
        >
          {{ tag }}
        </el-tag>
      </div>
    </div>

    <!-- 搜索结果 -->
    <div v-if="keyword" class="search-results">
      <!-- 筛选栏 -->
      <div class="filter-bar">
        <el-radio-group v-model="type" @change="handleTypeChange">
          <el-radio-button label="all">全部</el-radio-button>
          <el-radio-button label="course">课程</el-radio-button>
          <el-radio-button label="article">文章</el-radio-button>
          <el-radio-button label="teacher">教师</el-radio-button>
        </el-radio-group>

        <el-select
          v-if="type === 'course'"
          v-model="courseFilter"
          placeholder="课程分类"
          clearable
          @change="handleFilterChange"
        >
          <el-option
            v-for="item in courseCategories"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>

        <el-select
          v-if="type === 'course'"
          v-model="courseSort"
          placeholder="排序方式"
          @change="handleSortChange"
        >
          <el-option label="最新发布" value="latest" />
          <el-option label="最多学习" value="popular" />
          <el-option label="最高评分" value="rating" />
        </el-select>
      </div>

      <!-- 课程列表 -->
      <div v-if="type === 'all' || type === 'course'" class="course-list">
        <el-card
          v-for="course in courses"
          :key="course.id"
          class="course-card"
          @click="handleCourseClick(course)"
        >
          <div class="course-cover">
            <el-image :src="course.cover" fit="cover" />
            <div class="course-tag" v-if="course.isNew">新课</div>
            <div class="course-tag hot" v-if="course.isHot">热门</div>
          </div>

          <div class="course-info">
            <h3 class="course-title">{{ course.title }}</h3>
            <p class="course-desc">{{ course.description }}</p>

            <div class="course-meta">
              <div class="teacher">
                <el-avatar :size="24" :src="course.teacher.avatar" />
                <span>{{ course.teacher.name }}</span>
              </div>

              <div class="stats">
                <span>
                  <el-icon><VideoPlay /></el-icon>
                  {{ course.lessonCount }}课时
                </span>
                <span>
                  <el-icon><User /></el-icon>
                  {{ course.studentCount }}人学习
                </span>
                <span>
                  <el-icon><Star /></el-icon>
                  {{ course.rating }}分
                </span>
              </div>
            </div>

            <div class="course-footer">
              <div class="tags">
                <el-tag
                  v-for="tag in course.tags"
                  :key="tag"
                  size="small"
                >
                  {{ tag }}
                </el-tag>
              </div>
              <div class="price">
                <span class="current">¥{{ course.price }}</span>
                <span class="original" v-if="course.originalPrice">
                  ¥{{ course.originalPrice }}
                </span>
              </div>
            </div>
          </div>
        </el-card>
      </div>

      <!-- 文章列表 -->
      <div v-if="type === 'all' || type === 'article'" class="article-list">
        <el-card
          v-for="article in articles"
          :key="article.id"
          class="article-card"
          @click="handleArticleClick(article)"
        >
          <div class="article-cover" v-if="article.cover">
            <el-image :src="article.cover" fit="cover" />
          </div>

          <div class="article-info">
            <h3 class="article-title">{{ article.title }}</h3>
            <p class="article-desc">{{ article.description }}</p>

            <div class="article-meta">
              <div class="author">
                <el-avatar :size="24" :src="article.author.avatar" />
                <span>{{ article.author.name }}</span>
              </div>

              <div class="stats">
                <span>
                  <el-icon><View /></el-icon>
                  {{ article.viewCount }}
                </span>
                <span>
                  <el-icon><ChatDotRound /></el-icon>
                  {{ article.commentCount }}
                </span>
                <span>
                  <el-icon><Star /></el-icon>
                  {{ article.likeCount }}
                </span>
              </div>

              <div class="time">
                {{ article.publishTime }}
              </div>
            </div>

            <div class="article-tags">
              <el-tag
                v-for="tag in article.tags"
                :key="tag"
                size="small"
              >
                {{ tag }}
              </el-tag>
            </div>
          </div>
        </el-card>
      </div>

      <!-- 教师列表 -->
      <div v-if="type === 'all' || type === 'teacher'" class="teacher-list">
        <el-card
          v-for="teacher in teachers"
          :key="teacher.id"
          class="teacher-card"
          @click="handleTeacherClick(teacher)"
        >
          <div class="teacher-avatar">
            <el-avatar :size="80" :src="teacher.avatar" />
            <div class="teacher-tag" v-if="teacher.isNew">新入驻</div>
          </div>

          <div class="teacher-info">
            <h3 class="teacher-name">{{ teacher.name }}</h3>
            <p class="teacher-title">{{ teacher.title }}</p>

            <div class="teacher-stats">
              <div class="stat-item">
                <span class="label">课程数</span>
                <span class="value">{{ teacher.courseCount }}</span>
              </div>
              <div class="stat-item">
                <span class="label">学生数</span>
                <span class="value">{{ teacher.studentCount }}</span>
              </div>
              <div class="stat-item">
                <span class="label">评分</span>
                <span class="value">{{ teacher.rating }}</span>
              </div>
            </div>

            <div class="teacher-tags">
              <el-tag
                v-for="tag in teacher.tags"
                :key="tag"
                size="small"
              >
                {{ tag }}
              </el-tag>
            </div>
          </div>
        </el-card>
      </div>

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[12, 24, 36, 48]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <!-- 搜索建议 -->
    <div v-else class="search-suggestions">
      <div class="suggestion-section">
        <h3>热门搜索</h3>
        <div class="suggestion-list">
          <el-tag
            v-for="item in hotSearches"
            :key="item"
            class="suggestion-tag"
            @click="handleSuggestionClick(item)"
          >
            {{ item }}
          </el-tag>
        </div>
      </div>

      <div class="suggestion-section">
        <h3>搜索历史</h3>
        <div class="suggestion-list">
          <el-tag
            v-for="item in searchHistory"
            :key="item"
            class="suggestion-tag"
            closable
            @click="handleSuggestionClick(item)"
            @close="handleRemoveHistory(item)"
          >
            {{ item }}
          </el-tag>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import {
  Search,
  VideoPlay,
  User,
  Star,
  View,
  ChatDotRound
} from '@element-plus/icons-vue'

const router = useRouter()

// 搜索关键词
const keyword = ref('')

// 搜索类型
const type = ref('all')

// 课程筛选
const courseFilter = ref('')
const courseSort = ref('latest')
const courseCategories = [
  { label: '前端开发', value: 'frontend' },
  { label: '后端开发', value: 'backend' },
  { label: '移动开发', value: 'mobile' },
  { label: '数据库', value: 'database' },
  { label: '人工智能', value: 'ai' }
]

// 分页
const currentPage = ref(1)
const pageSize = ref(12)
const total = ref(0)

// 热门标签
const hotTags = [
  'Vue.js',
  'React',
  'Node.js',
  'Python',
  'Java',
  'Go',
  'Docker',
  'Kubernetes'
]

// 热门搜索
const hotSearches = [
  'Vue3 实战教程',
  'React Hooks 入门',
  'Node.js 后端开发',
  'Python 数据分析',
  'Java 微服务架构'
]

// 搜索历史
const searchHistory = ref([])

// 搜索结果
const courses = ref([])
const articles = ref([])
const teachers = ref([])

// 搜索
const handleSearch = () => {
  if (!keyword.value) return
  
  // 保存搜索历史
  if (!searchHistory.value.includes(keyword.value)) {
    searchHistory.value.unshift(keyword.value)
    if (searchHistory.value.length > 10) {
      searchHistory.value.pop()
    }
  }
  
  // TODO: 调用搜索 API
  fetchSearchResults()
}

// 获取搜索结果
const fetchSearchResults = () => {
  // TODO: 根据类型和筛选条件获取搜索结果
  // 模拟数据
  courses.value = [
    {
      id: 1,
      title: 'Vue3 实战教程',
      description: '从入门到精通的 Vue3 课程',
      cover: 'https://example.com/course1.jpg',
      isNew: true,
      isHot: true,
      teacher: {
        name: '张老师',
        avatar: 'https://example.com/avatar1.jpg'
      },
      lessonCount: 30,
      studentCount: 1000,
      rating: 4.8,
      tags: ['Vue3', '前端', '实战'],
      price: 199,
      originalPrice: 299
    }
  ]
  
  articles.value = [
    {
      id: 1,
      title: 'Vue3 组合式 API 最佳实践',
      description: '深入理解 Vue3 组合式 API 的使用方法',
      cover: 'https://example.com/article1.jpg',
      author: {
        name: '李老师',
        avatar: 'https://example.com/avatar2.jpg'
      },
      viewCount: 5000,
      commentCount: 100,
      likeCount: 200,
      publishTime: '2024-01-01',
      tags: ['Vue3', '前端', '最佳实践']
    }
  ]
  
  teachers.value = [
    {
      id: 1,
      name: '王老师',
      avatar: 'https://example.com/avatar3.jpg',
      isNew: true,
      title: '资深前端工程师',
      courseCount: 10,
      studentCount: 5000,
      rating: 4.9,
      tags: ['Vue', 'React', 'Node.js']
    }
  ]
  
  total.value = 100
}

// 切换类型
const handleTypeChange = () => {
  currentPage.value = 1
  fetchSearchResults()
}

// 切换筛选
const handleFilterChange = () => {
  currentPage.value = 1
  fetchSearchResults()
}

// 切换排序
const handleSortChange = () => {
  currentPage.value = 1
  fetchSearchResults()
}

// 切换页码
const handleCurrentChange = (page) => {
  currentPage.value = page
  fetchSearchResults()
}

// 切换每页条数
const handleSizeChange = (size) => {
  pageSize.value = size
  currentPage.value = 1
  fetchSearchResults()
}

// 点击标签
const handleTagClick = (tag) => {
  keyword.value = tag
  handleSearch()
}

// 点击搜索建议
const handleSuggestionClick = (suggestion) => {
  keyword.value = suggestion
  handleSearch()
}

// 移除搜索历史
const handleRemoveHistory = (item) => {
  const index = searchHistory.value.indexOf(item)
  if (index > -1) {
    searchHistory.value.splice(index, 1)
  }
}

// 点击课程
const handleCourseClick = (course) => {
  router.push(`/course/${course.id}`)
}

// 点击文章
const handleArticleClick = (article) => {
  router.push(`/article/${article.id}`)
}

// 点击教师
const handleTeacherClick = (teacher) => {
  router.push(`/teacher/${teacher.id}`)
}

// 初始化
onMounted(() => {
  // 从本地存储加载搜索历史
  const history = localStorage.getItem('searchHistory')
  if (history) {
    searchHistory.value = JSON.parse(history)
  }
})
</script>

<style scoped>
.search-container {
  padding: 20px;
}

.search-header {
  max-width: 800px;
  margin: 0 auto 40px;
}

.search-input {
  margin-bottom: 20px;
}

.search-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.hot-tag {
  cursor: pointer;
}

.filter-bar {
  display: flex;
  gap: 20px;
  margin-bottom: 20px;
}

.course-list,
.article-list,
.teacher-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
  margin-bottom: 20px;
}

.course-card,
.article-card,
.teacher-card {
  cursor: pointer;
  transition: transform 0.3s;
}

.course-card:hover,
.article-card:hover,
.teacher-card:hover {
  transform: translateY(-5px);
}

.course-cover {
  position: relative;
  height: 200px;
}

.course-cover .el-image {
  width: 100%;
  height: 100%;
}

.course-tag {
  position: absolute;
  top: 10px;
  right: 10px;
  padding: 2px 8px;
  background-color: var(--el-color-primary);
  color: white;
  border-radius: 4px;
  font-size: 12px;
}

.course-tag.hot {
  background-color: var(--el-color-danger);
}

.course-info {
  padding: 15px;
}

.course-title {
  margin: 0 0 10px;
  font-size: 16px;
  font-weight: bold;
}

.course-desc {
  margin: 0 0 15px;
  color: var(--el-text-color-secondary);
  font-size: 14px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.course-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.teacher {
  display: flex;
  align-items: center;
  gap: 5px;
}

.stats {
  display: flex;
  gap: 15px;
  color: var(--el-text-color-secondary);
  font-size: 14px;
}

.stats span {
  display: flex;
  align-items: center;
  gap: 5px;
}

.course-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.tags {
  display: flex;
  gap: 5px;
}

.price {
  display: flex;
  align-items: baseline;
  gap: 5px;
}

.current {
  color: var(--el-color-danger);
  font-size: 18px;
  font-weight: bold;
}

.original {
  color: var(--el-text-color-secondary);
  font-size: 14px;
  text-decoration: line-through;
}

.article-card {
  display: flex;
  gap: 20px;
}

.article-cover {
  width: 200px;
  height: 150px;
}

.article-cover .el-image {
  width: 100%;
  height: 100%;
}

.article-info {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.article-title {
  margin: 0 0 10px;
  font-size: 16px;
  font-weight: bold;
}

.article-desc {
  margin: 0 0 15px;
  color: var(--el-text-color-secondary);
  font-size: 14px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.article-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.author {
  display: flex;
  align-items: center;
  gap: 5px;
}

.time {
  color: var(--el-text-color-secondary);
  font-size: 14px;
}

.article-tags {
  display: flex;
  gap: 5px;
}

.teacher-card {
  display: flex;
  gap: 20px;
  padding: 20px;
}

.teacher-avatar {
  position: relative;
}

.teacher-tag {
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  padding: 2px 8px;
  background-color: var(--el-color-primary);
  color: white;
  border-radius: 4px;
  font-size: 12px;
}

.teacher-info {
  flex: 1;
}

.teacher-name {
  margin: 0 0 5px;
  font-size: 18px;
  font-weight: bold;
}

.teacher-title {
  margin: 0 0 15px;
  color: var(--el-text-color-secondary);
  font-size: 14px;
}

.teacher-stats {
  display: flex;
  gap: 20px;
  margin-bottom: 15px;
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.stat-item .label {
  color: var(--el-text-color-secondary);
  font-size: 12px;
}

.stat-item .value {
  font-size: 16px;
  font-weight: bold;
}

.teacher-tags {
  display: flex;
  gap: 5px;
}

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 40px;
}

.search-suggestions {
  max-width: 800px;
  margin: 0 auto;
}

.suggestion-section {
  margin-bottom: 30px;
}

.suggestion-section h3 {
  margin: 0 0 15px;
  font-size: 16px;
  color: var(--el-text-color-primary);
}

.suggestion-list {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.suggestion-tag {
  cursor: pointer;
}
</style> 